package com.tpp.utils;

import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import org.json.JSONException;

import java.io.IOException;
import java.util.Random;

/**
 * @Package: com.xyym.utils
 * @ClassName: CodeUtils
 * @Author: myh
 * @CreateTime: 2021/7/25 11:41
 * @Description:
 */
public class CodeUtils {
//    public static void main(String[] args) {
    public static String getCodeDx(String phone) {
        // 短信应用SDK AppID
        int appid = 1400551947; // 1400开头

        // 短信应用SDK AppKey
        String appkey = "b29b9ae6ce4cb2284fedf9de4fa22a48";

        // 需要发送短信的手机号码(可以是多个)
//        String[] phoneNumbers = {"18403426879","13363986651","15819267570"};
        String[] phoneNumbers = {phone};


        // 短信模板ID，需要在短信应用中申请
        // NOTE: 这里的模板ID`7839`只是一个示例，
        // 真实的模板ID需要在短信控制台中申请
        int templateId = 1048509;

        // 签名
        // NOTE: 这里的签名"腾讯云"只是一个示例，
        // 真实的签名需要在短信控制台中申请，另外
        // 签名参数使用的是`签名内容`，而不是`签名ID`
        String smsSign = "清水泉";

        String verifyCode=null;
        try {

            //生成6位验证码
            verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);

            String[] params = {verifyCode, "5"};
//            第一个收到的验证码，第二个是几分钟内回复
            SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
            SmsSingleSenderResult result = ssender.sendWithParam("86", phoneNumbers[0],
                    templateId, params, smsSign, "", "");
            System.out.println(result + "完成了");
        } catch (HTTPException e) {
            // HTTP 响应码错误
            System.out.println("响应码错误");
            e.printStackTrace();
        } catch (JSONException e) {
            System.out.println(" 解析错误");
            // JSON 解析错误
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println(" 网络 IO 错误");
            // 网络 IO 错误
            e.printStackTrace();
        }
        return verifyCode;
    }


/*//    youjianfasong

    @Value("${spring.mail.username}")
    private static String from;

    public static SimpleMailMessage sendMail(String email) {

        //生成一个随机验证码
        Random random = new Random();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i=0;i<6;i++){
            stringBuffer.append(random.nextInt(10));
        }

        //邮件的发送
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setTo(email);
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setSubject("xiachufang验证码");
        simpleMailMessage.setText(stringBuffer.toString());

        return simpleMailMessage;
    }*/
}
